package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
// @Slf4j可以自动创建日志记录对象
@Slf4j
public class DeptController {

    //创建日志记录对象
   // private static Logger logger = LoggerFactory.getLogger(DeptController.class);

    @Autowired
    private DeptService deptService;

    // 查询所有部门
    //@RequestMapping(value = "/depts",method = RequestMethod.GET)
    @GetMapping("/depts")
    public Result list() {
        log.info("查询所有部门信息. . .");
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    // 根据id删除部门
    @DeleteMapping("/depts/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("根据id："+id+"删除部门. . .");
        deptService.delete(id);
        return Result.success();
    }

    // 添加部门
    @PostMapping("/depts")
    public Result add(@RequestBody Dept dept){
        log.info("新增部门：{}",dept);
        deptService.add(dept);
        return Result.success();
    }

    // 根据id查询部门信息
    @GetMapping("depts/{id}")
    public Result findById(@PathVariable Integer id) {
        log.info("根据id：{}查询部门信息. . .",id);
        Dept dept = deptService.findById(id);
        return Result.success(dept);
    }

    // 根据id修改部门信息
    @PutMapping("/depts")
    public Result update(@RequestBody Dept dept){
        log.info("根据id：{}修改部门信息. . .",dept.getId());
        deptService.update(dept);
        return Result.success();
    }
}
